package com.spzx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.service.RemoteProductSkuService;
import com.spzx.user.api.domain.UpdateUserLogin;
import com.spzx.user.api.domain.UserBrowseHistory;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.user.domain.UserCollect;
import com.spzx.user.mapper.UserBrowseHistoryMapper;
import com.spzx.user.mapper.UserCollectMapper;
import com.spzx.user.mapper.UserInfoMapper;
import com.spzx.user.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员Service业务层处理
 *
 * @author atguigu
 * @date 2024-11-02
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserCollectMapper userCollectMapper;

    @Autowired
    private UserBrowseHistoryMapper userBrowseHistoryMapper;

    @Autowired
    private RemoteProductSkuService remoteProductSkuService;

    /**
     * 查询会员列表
     *
     * @param userInfo 会员
     * @return 会员
     */
    @Override
    public List<UserInfo> selectUserInfoList(UserInfo userInfo) {
        return userInfoMapper.selectUserInfoList(userInfo);
    }

    @Override
    public void register(UserInfo userInfo) {
        //首先查询该用户是否存在
        Long count = userInfoMapper.selectCount(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUsername, userInfo.getUsername()));
        //判断
        if (count > 0) {
            throw new ServiceException("该用户已经存在");
        }
        //添加该用户
        userInfoMapper.insert(userInfo);
    }

    @Override
    public UserInfo getUserInfoByUsername(String username) {
        UserInfo userInfo = baseMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUsername, username));
        return userInfo;
    }

    @Override
    public Boolean updateUserLogin(UpdateUserLogin updateUserLogin) {
        //创建UserInfo对象
        UserInfo userInfo = new UserInfo();
        //设置主键
        userInfo.setId(updateUserLogin.getUserId());
        userInfo.setLastLoginIp(updateUserLogin.getLastLoginIp());
        userInfo.setUpdateTime(updateUserLogin.getLastLoginTime());
        //调用根据id更新的方法
        int update = baseMapper.updateById(userInfo);
        return update > 0;
    }

    @Override
    public Boolean isCollect(Long skuId) {
        //获取userId
        Long userId = SecurityUtils.getUserId();
        //根据userId和商品id查询数据库
        UserCollect userCollect = userCollectMapper.selectOne(new LambdaQueryWrapper<UserCollect>().eq(UserCollect::getUserId, userId).eq(UserCollect::getSkuId, skuId));
        return userCollect != null;
    }

    @Override
    public Boolean collect(Long skuId) {
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //创建UserCollect对象
        UserCollect userCollect = new UserCollect();
        userCollect.setUserId(userId);
        userCollect.setSkuId(skuId);
        //向数据库插入数据
        int insert = userCollectMapper.insert(userCollect);
        return insert > 0;

    }

    @Override
    public Boolean cancelCollect(Long skuId) {
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //调用带条件的删除的方法
        int delete = userCollectMapper.delete(new LambdaQueryWrapper<UserCollect>().eq(UserCollect::getUserId, userId).eq(UserCollect::getSkuId, skuId));
        return delete > 0;
    }

    @Override
    public List<ProductSku> getUserBrowseHistoryList() {
        //获取用户Id
        Long userId = SecurityUtils.getUserId();
        //根据用户id获取用户所有的浏览记录
        List<UserBrowseHistory> userBrowseHistories = userBrowseHistoryMapper.selectList(new LambdaQueryWrapper<UserBrowseHistory>().eq(UserBrowseHistory::getUserId, userId).orderByDesc(UserBrowseHistory::getUpdateTime));
        //创建一个返回的集合
        ArrayList<ProductSku> productSkuList = new ArrayList<>();
        //获取所有的skuId
        List<Long> skuIdList = userBrowseHistories.stream().map(userBrowseHistory ->
                userBrowseHistory.getSkuId()
        ).toList();
        skuIdList.forEach(skuId -> {
            ProductSku productSku = remoteProductSkuService.getProductSkuById(skuId, SecurityConstants.INNER).getData();
            productSkuList.add(productSku);
        });
        return productSkuList;
    }

}
